package com.ruoyi.rtsp.service.impl;

import com.ruoyi.camera.util.RtspPackage;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.rtsp.component.WebSocketServer;
import com.ruoyi.rtsp.service.RtspService;
import com.ruoyi.rtsp.util.Image;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

/**
 *  获取rtsp流，抓取每帧，通过websocket传递给前台显示
 */
@Slf4j
@Service
@EnableAsync
public class RtspServiceImpl implements RtspService {

    @Value("${rtsp.transport.type:tcp}")
    private String rtspTransportType;

    @Autowired
    private RtspPackage rtspPackage;

//    private  String test = "rtsp://admin:abc123456@8.130.182.184:9554/LiveMedia/ch1/Media1";


    /**
     * 异步开启获取rtsp流，通过websocket传输数据
     */
    @Async
    @Override
    public void live(String id, String type) {


        String rtspUrl = rtspPackage.rtspBuild(id, type);

        log.info("开始创建grabber");
        FFmpegFrameGrabber grabber = null;
        try {
            grabber = new FFmpegFrameGrabber(rtspUrl);
            grabber.setOption("rtsp_transport", rtspTransportType);
            //设置帧率
            grabber.setFrameRate(25);
            //设置获取的视频宽度
            grabber.setImageWidth(996);
            //设置获取的视频高度
            grabber.setImageHeight(996);
            //设置视频bit率
            grabber.setVideoBitrate(2000000);
            //设置日志等级
            avutil.av_log_set_level(avutil.AV_LOG_ERROR);
            grabber.start();
            log.info("创建并启动grabber成功");
        }catch (Exception e){
            e.printStackTrace();
        }

        //推送图片
        Java2DFrameConverter java2DFrameConverter = new Java2DFrameConverter();
        while (true) {
            try {
                if (grabber != null) {
                    Frame frame = grabber.grabImage();
                    if (null == frame) {
                        continue;
                    }
                    BufferedImage bufferedImage = java2DFrameConverter.getBufferedImage(frame);
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    ImageIO.write(bufferedImage, "jpg", out);
                    //使用websocket发送视频帧数据
                    WebSocketServer.sendObject(new Image(out.toByteArray()), id);
                }
            } catch (Exception e) {
                e.printStackTrace();
                if (grabber != null) {
                    try {
                        grabber.stop();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    } finally {
                        grabber = null;
                    }
                }
            }
        }
    }

}